package managedbeans;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import model.Agenda;
import model.ParcelaDespesa;
import model.ParcelaReceita;
import model.Usuario;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import factoryService.FactoryService;

@ManagedBean
@ViewScoped
public class ScheduleController implements Serializable {

	private static final long serialVersionUID = 5698947466264054954L;
	private ScheduleModel eventModel;
	private ScheduleModel lazyEventModel;
	private ScheduleEvent event = new DefaultScheduleEvent();
	private String theme;
	private Agenda agenda;
	private Usuario usuario;
	private List<Agenda> listaAgenda;
	private String title;
	private Date start;
	private Date end;
	private Date horaStart;
	private Date horaEnd;
	private List<ParcelaReceita> listaParcelaReceita;
	private List<ParcelaDespesa> listaParcelaDespesa;
	
	@SuppressWarnings("rawtypes")
	public ScheduleController() {
		if (agenda == null) {
			agenda = new Agenda();
		}
		if (usuario == null) {
			usuario = (Usuario) FacesContext.getCurrentInstance()
					.getExternalContext().getSessionMap().get("usuario");
		}
		listaParcelaReceita = FactoryService.getParcelaReceitaService().findAll(usuario.getCodigo());
		listaParcelaDespesa = FactoryService.getParcelaDespesaService().findAll(usuario.getCodigo());
		setListaAgenda(FactoryService.getAgendaService().findAll(usuario.getCodigo()));
		
		eventModel = new DefaultScheduleModel();
		
		
		Iterator it1 = listaParcelaReceita.iterator();  
		while (it1.hasNext()) {  
		    ParcelaReceita item = (ParcelaReceita) it1.next();  
		    eventModel.addEvent(new DefaultScheduleEvent("Receita:"+item.getReceita().getDescricao()+" R$:"+item.getValor(),item.getDataParcela(),item.getDataParcela(),true));
		}
		Iterator it2 = listaParcelaDespesa.iterator();  
		while (it2.hasNext()) {  
		    ParcelaDespesa item = (ParcelaDespesa) it2.next();  
		    eventModel.addEvent(new DefaultScheduleEvent("Despesa:"+item.getDespesa().getDescricao()+" R$:"+item.getValor(),item.getDataParcela(),item.getDataParcela(),true));
		}
		Iterator it = listaAgenda.iterator();  
		while (it.hasNext()) {  
		    Agenda item = (Agenda) it.next();  
		    eventModel.addEvent(new DefaultScheduleEvent(item.getTitle(),item.getStartDate(),item.getEndDate(),item.isAllDay()));
		    
		}
		

		lazyEventModel = new LazyScheduleModel() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 212911404670552287L;

			@SuppressWarnings("unused")
			public void fetchEvents(Date start, Date end) {
				clear();

				Date random = getRandomDate(start);
				addEvent(new DefaultScheduleEvent("Lazy Event 1", random,
						random));

				random = getRandomDate(start);
				addEvent(new DefaultScheduleEvent("Lazy Event 2", random,
						random));
			}
		};
	}

	

	public Date getRandomDate(Date base) {
		Calendar date = Calendar.getInstance();
		date.setTime(base);
		date.add(Calendar.DATE, ((int) (Math.random() * 30)) + 1); // set random
																	// day of
																	// month

		return date.getTime();
	}

	public Date getInitialDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY,
				calendar.get(Calendar.DATE), 0, 0, 0);

		return calendar.getTime();
	}

	public ScheduleModel getEventModel() {
		return eventModel;
	}

	public ScheduleModel getLazyEventModel() {
		return lazyEventModel;
	}

	public ScheduleEvent getEvent() {
		return event;
	}

	public void setEvent(ScheduleEvent event) {
		this.event = event;
	}

	public void agenda(ScheduleEvent event, Date c, Date c2) {
		agenda.setAllDay(event.isAllDay());
		agenda.setTitle(event.getTitle());
		agenda.setStartDate(c);
		agenda.setEndDate(c2);
		agenda.setUsuario(usuario);
		
	}

	@SuppressWarnings("deprecation")
	public void addEvent(ActionEvent actionEvent) {
		
		event.getStartDate().setHours(horaStart.getHours()+3);
		event.getStartDate().setMinutes(horaStart.getMinutes());
		event.getEndDate().setHours(horaEnd.getHours()+3);
		event.getEndDate().setMinutes(horaEnd.getMinutes());
		
		System.out.println(event.getStartDate()+"  "+event.getEndDate());
		
		Calendar c = Calendar.getInstance();
		c.setTime(event.getStartDate());
		c.add(Calendar.DATE, 1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(event.getEndDate());
		c2.add(Calendar.DATE, 1);
		
		if (event.getId() == null) {
			eventModel.addEvent(new DefaultScheduleEvent(event.getTitle(), c
					.getTime(), c2.getTime(), event.isAllDay()));
			agenda(event, c.getTime(), c2.getTime());
			FactoryService.getAgendaService().salvar(agenda);
		} else {
			eventModel.updateEvent(new DefaultScheduleEvent(event.getTitle(), c
					.getTime(), c2.getTime(), event.isAllDay()));
			agenda = FactoryService.getAgendaService().procuraAgendaBy(title,
					usuario.getCodigo(), start, end);
			agenda(event, c.getTime(), c2.getTime());
		}
		agenda = new Agenda();
		event = new DefaultScheduleEvent();
		horaStart = null;
		horaEnd = null;

	}

	public void delEvent(ActionEvent actionEvent) {
		agenda = FactoryService.getAgendaService().procuraAgendaBy(title,
				usuario.getCodigo(), start, end);
		FactoryService.getAgendaService().remover(agenda);
		eventModel.deleteEvent(event);

	}

	public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
		System.out.println("EVENT SELECT");
		event = selectEvent.getScheduleEvent();
		title = event.getTitle();
		start = event.getStartDate();
		end = event.getEndDate();
		horaStart = event.getStartDate();
		horaEnd = event.getEndDate();
	}

	public void onDateSelect(DateSelectEvent selectEvent) {
		System.out.println("DATE SELECT");
		event = new DefaultScheduleEvent("", selectEvent.getDate(),
				selectEvent.getDate());
	}

	public void onEventMove(ScheduleEntryMoveEvent event) {
		System.out.println("EVENT MOVE: " + event.getDayDelta()
				+ event.getMinuteDelta());
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		start.setTime(event.getScheduleEvent().getStartDate());
		end.setTime(event.getScheduleEvent().getEndDate());
		start.add(Calendar.DAY_OF_MONTH, (event.getDayDelta() / 10));
		start.add(Calendar.MINUTE, event.getMinuteDelta());
		end.add(Calendar.DAY_OF_MONTH, (event.getDayDelta() / 10));
		end.add(Calendar.MINUTE, event.getMinuteDelta());
		// agenda =
		// FactoryService.getAgendaService().procuraAgendaBy(event.getScheduleEvent().getTitle(),usuario.getCodigo());
		agenda.setStartDate(start.getTime());
		agenda.setEndDate(end.getTime());
		FactoryService.getAgendaService().salvar(agenda);
	}

	public void onEventResize(ScheduleEntryResizeEvent event) {
		System.out.println("EVENT RECIZABLE");
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		start.setTime(event.getScheduleEvent().getStartDate());
		end.setTime(event.getScheduleEvent().getEndDate());
		start.add(Calendar.DAY_OF_MONTH, (event.getDayDelta() / 10));
		start.add(Calendar.MINUTE, event.getMinuteDelta());
		end.add(Calendar.DAY_OF_MONTH, (event.getDayDelta() / 10));
		end.add(Calendar.MINUTE, event.getMinuteDelta());
		// agenda =
		// FactoryService.getAgendaService().procuraAgendaBy(event.getScheduleEvent().getTitle(),usuario.getCodigo());
		agenda.setStartDate(start.getTime());
		agenda.setEndDate(end.getTime());
		FactoryService.getAgendaService().salvar(agenda);
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public Agenda getAgenda() {
		return agenda;
	}

	public void setAgenda(Agenda agenda) {
		this.agenda = agenda;
	}

	public List<Agenda> getListaAgenda() {
		return listaAgenda;
	}

	public void setListaAgenda(List<Agenda> listaAgenda) {
		this.listaAgenda = listaAgenda;
	}



	public Date getHoraStart() {
		return horaStart;
	}



	public void setHoraStart(Date horaStart) {
		this.horaStart = horaStart;
	}



	public Date getHoraEnd() {
		return horaEnd;
	}



	public void setHoraEnd(Date horaEnd) {
		this.horaEnd = horaEnd;
	}


	
}
